package edu.scau.sku.service;

import edu.scau.core.Enum.SkuRecordType;
import edu.scau.core.util.ServletUtils;
import edu.scau.sku.domain.SkuRecord;
import edu.scau.sku.mapper.SkuRecordMapper;
import edu.scau.sku.vo.SkuRecordVo;
import edu.scau.system.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class SkuRecordServiceImpl implements SkuRecordService{
  @Autowired
  private TokenService tokenService;
  @Autowired
  private SkuRecordMapper skuRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRecord(Map<String, Integer> skuMap, int type) {
        String creator=tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        Date time=new Date();
        SkuRecord skuRecord=new SkuRecord();
        skuRecord.setCreator(creator);
        skuRecord.setCreateTime(time);
        skuRecord.setCreateBy(creator);
        skuRecord.setTime(time);
        skuRecord.setType(type);
        for (String skuCode : skuMap.keySet()) {
            skuRecord.setSkuCode(skuCode);
            skuRecord.setQuantity(skuMap.get(skuCode));
            this.insert(skuRecord);
        }
    }

    @Override
    public int insert(SkuRecord record) {
        String createBy = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        record.setCreateBy(createBy);
        Date createTime = new Date();
        record.setCreateTime(createTime);
        return skuRecordMapper.insert(record);
    }

    @Override
    public List<SkuRecordVo> listAll() {
        return skuRecordMapper.listAll();
    }


}
